package code

import (
	"context"
	"encoding/base64"
	"encoding/json"
	"io"
	"os"

	"github.com/docker/docker/api/types/image"
	"github.com/docker/docker/api/types/registry"
)

// 使用身份验证拉取镜像
func main() {
	cli := InitClient()

	// 身份校验
	ctx := context.Background()
	authConfig := registry.AuthConfig{
		Username: "username",
		Password: "password",
	}
	encodeJson, err := json.Marshal(authConfig)
	if err != nil {
		panic(err)
	}
	authStr := base64.URLEncoding.EncodeToString(encodeJson)

	// 拉取镜像
	out, err := cli.ImagePull(ctx, "mysql", image.PullOptions{RegistryAuth: authStr})
	if err != nil {
		panic(err)
	}
	defer out.Close()

	io.Copy(os.Stdout, out)
}
